package com.esun.cheetah.view.personal.webview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.HomeAct;
import com.esun.cheetah.view.personal.PersonalAct;
import com.esun.cheetah.view.personal.recharg.MyWebChromeClient;

/**
 * 
 * @Package com.esun.cheetah.view.personal.webview.WebviewActivity
 * @ClassName: WebviewActivity
 * @Description: TODO(通用Webview)
 * @author 程相亭 <br/>
 * @date create at 2012-7-10 下午8:07:21
 * 
 */
public class WebviewActivity extends Activity implements OnClickListener{
	
	protected WebView web ;
	private Button btn ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.common_web_act);

		Intent intent = getIntent();

		String url = intent.getStringExtra("url");
		String title = intent.getStringExtra("title");

		((TextView) this.findViewById(R.id.common_web_act_tv_title)).setText(title);
		
		btn = (Button)findViewById(R.id.btn_unlock_goback) ;

		btn.setOnClickListener(this);

		web = (WebView) this.findViewById(R.id.commen_web_web);
		web.getSettings().setJavaScriptEnabled(true);
		web.getSettings().setSaveFormData(false);
		web.getSettings().setSavePassword(false);
		web.getSettings().setSupportZoom(false);
		web.setBackgroundColor(0);
		web.setBackgroundResource(R.drawable.anull_icon);
		web.setVerticalScrollBarEnabled(false);
		web.setWebChromeClient(new MyWebChromeClient()) ;
		
		web.setWebViewClient(new WebViewClient() {  
      
      @Override  
      public boolean shouldOverrideUrlLoading(WebView view, String url) {  
          view.loadUrl(url);  
          return true;  
      }  
		});  

		web.addJavascriptInterface(new JSCallBack(), "client") ;
		web.loadUrl(url);
	}

	@Override
	public void onClick(View v) {
		forWard() ;
	}
	
	//js回调，跳转个人中心
	class JSCallBack {
		public void onUnlockSuccess(final String username, final String money, final String ck,
				final String mobile, final String password) {
			
			// 存储个人信息
			UserInfo.getUserInfo().setCk(ck) ;
			UserInfo.getUserInfo().setUserName(username) ;
			UserInfo.getUserInfo().setMoney(money) ;
			UserInfo.getUserInfo().setBindmobile(mobile) ;
			
			SharedPreferences sharedPreferences = getSharedPreferences(Key.SHAREDPREFERENCES_NAME,
					Context.MODE_PRIVATE);
			Editor editor = sharedPreferences.edit();
			editor.putString(Key.KEY_USERNAME, username);
			editor.putString(Key.SAVE_PASSWORD, password);
			editor.commit();
						
			Intent intent = new Intent();
			intent.setClass(WebviewActivity.this, HomeAct.class);
			PersonalAct.flag = Language.FLAG_SIX;
			HomeAct.flag = Language.FLAG_ONE;
			startActivity(intent);
			WebviewActivity.this.finish();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			forWard() ;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void forWard () {
		Intent intent = new Intent();
		intent.setClass(WebviewActivity.this, HomeAct.class);
		PersonalAct.flag = Language.FLAG_FIVE;
		HomeAct.flag = Language.FLAG_ONE;
		startActivity(intent);
		WebviewActivity.this.finish();
		this.finish() ;
	}
}
